<?php


namespace app\unify\validate;


use think\Validate;
use app\unify\model\Menu as MenuModel;

class Menu extends Validate
{

    const SAVE = "Save";

    const UPDATE = 'Update';

    const DELETE = 'Delete';
    const UPDATE_FIELD =  "UpdateField";

    protected $rule = [
        'id|编号' => 'require|integer',
        'type|类型' => 'require|in:' . MenuModel::TYPE_GROUP . ',' . MenuModel::TYPE_PAGE . ',' . MenuModel::TYPE_MENU_PAGE . ',' . MenuModel::TYPE_MENU_LINK,
        'pid|父菜单' => 'require|integer',
        'title|标题' => 'require|length:0,20',
        'icon|图标' => 'requireCallback:checkIconRequire|length:0,80',
        'url|地址' => 'length:0,200|requireCallback:checkTypeRequire',
        'param|参数' => 'length:0,200',
        'target|打开方式' => 'length:0,20|requireCallback:checkTypeRequire',
        'serial|排序号' => 'require|number',
        'debug|仅开发可见' => 'requireCallback:checkTypeRequire|in:' . MenuModel::DEBUG_FALSE . "," . MenuModel::DEBUG_TRUE,
        'status|状态' => 'require|in:' . MenuModel::STATUS_DISABLE . ',' . MenuModel::STATUS_ENABLE,
        'ids|编号' => 'require|array',
        'field|字段' => 'require|in:status,serial'
    ];

    protected $message = [
    ];

    protected $scene = [
        self::SAVE => ['pid', 'title', 'icon', 'url', 'param', 'target', 'serial', 'debug', 'status'],
        self::UPDATE => ['id', 'pid', 'title', 'icon', 'url', 'param', 'target', 'serial', 'debug', 'status'],
        self::DELETE => ['ids'],
        self::UPDATE_FIELD => ['ids', 'field', 'value']
    ];


    public function checkTypeRequire($value, $data)
    {
        return $data['type'] != MenuModel::TYPE_GROUP;
    }

    public function checkIconRequire($value, $data)
    {
        return in_array($data['type'], [
            MenuModel::TYPE_GROUP,
            MenuModel::TYPE_MENU_PAGE,
            MenuModel::TYPE_MENU_LINK,
        ]);
    }
}